<?php

class Apporder_model extends Base_Model
{
    Const TABLE = 'app_order';

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    /**
     * 获取信息
     *
     * @param $data
     * @return mixed
     */
    public function orderlist($data, $offset, $limit)
    {
        $this->db->select(
                          'id,
                           title,
                           startDay,
                           stopDay,
                           persons,
                           moneyTotalRMB,
                           status'
        );
        $this->db->from(self::TABLE)->where($data)->order_by('created');
        $this->db->limit($limit, $offset);

        $data = $this->db->get()->result_array();
        if($data){
            foreach($data as $key => &$val){
                $val['status'] = self::orderStatusName($val['status']);
            }
        }

        return $data;
    }

    /**
     * 处理列表
     *
     * @param $where
     * @return mixed
     */
    function getDealOrder($where)
    {
        $sql = <<<EOT
SELECT
	a.id order_id,
	a.startDay,
	a.stopDay,
	a.moneyTotal,
	a.status as status_id,
	a.status,
	c. name title,
	c.thumb
FROM
	app_order a
LEFT JOIN (
	SELECT
		id,
		uid,
		`name`,
		thumb,
		'ACTIVITY' type
	FROM
		mod_activity
	UNION ALL
		SELECT
			id,
			uid,
			`name`,
			thumb,
			'TRACK' type
		FROM
			mod_track
	UNION ALL
		SELECT
			id,
			uid,
			`name`,
			thumb,
			'TRACK' type
		FROM
			mod_ziying
) c ON c.id = a.goodsId
AND c.type = a.goodsType WHERE
EOT;
        $sql .= $where;
        return $this->db->query($sql)->result_array();

    }

    /**
     * 订单详情
     *
     * @param $data
     * @return mixed
     */
    public function orderDetail($data, $type = Null)
    {
        $this->db->select(
                          'ao.id as order_id,
                           ao.title,
                           ao.startDay,
                           ao.stopDay,
                           ao.persons,
                           ao.moneyTotal,
                           ao.status,
                           ao.orderNo,
                           ao.created,
                           ao.shopId,
                           u.telNum,
                           u.telCode,
                           u.userName,
                           u.email'
        );
        $this->db->from(self::TABLE.' as ao')->where($data);
        $this->db->join('user_v2 as u', 'u.uid = ao.uid', 'left');
        $this->db->where($data);

        $data = $this->db->get()->row_array();
        if($data){
            $data['status_id'] = $data['status'];
            if(!$type){
                $data['status'] = self::orderStatusName($data['status']);
            }else{
                $status = self::dealOrderStatusName($data['status']);
                $data = array_merge($data, $status);
            }
            $data['created'] = date('Y-m-d H:i:s', $data['created']);
        }

        return $data;
    }

    /**
     * 更新订单
     *
     * @param $data
     * @param $where
     * @return CI_DB_active_record|CI_DB_result
     */
    public function updateOrder($data, $where)
    {
        $this->db->set($data);
        $this->db->where($where);
        return $this->db->update(self::TABLE);
    }


    /**
     * 订单状态
     *
     * @param $statusId
     * @return string
     */
    public static function orderStatusName ($statusId)
    {
        switch ($statusId)
        {
            case 0:
                return '等待付款';
                break;

            // 已付款
            case 1:
                return '等待确认';
                break;

            case 2:
                return '玩伴拒绝';
                break;

            // 导游同意后
            case 3:
                return '行程中';
                break;

            // 退款中
            case 31:
                return '退款中';
                break;

            // 行程完成
            case 99:
                return '待评价';
                break;

            // 退款流程
            case 4:
                return '退款中';
                break;

            case 5:
                return '退款中';
                break;

            case 6:
                return '已退款';
                break;

            case 32:
                return '已退款';
                break;
            // 作废流程
            case 7:
                return '已完成';
                break;
        }
    }

    /**
     * 订单状态
     *
     * @param $statusId
     * @return string
     */
    public static function dealOrderStatusName ($statusId)
    {
        switch ($statusId)
        {
            case 0:
                return array('status' => '等待付款', 'contents' => '');
                break;

            // 已付款
            case 1:
                return array('status' => '付款成功', 'contents' => '等待玩伴处理申请');
                break;

            case 2:
                return array('status' => '申请被玩伴拒绝', 'contents' => '哇吖会将费用原路退回您的支付宝或微信');
                break;

            // 导游同意后
            case 3:
                return array('status' => '', 'contents' => '');
                break;

            // 退款中
            case 31:
                return array('status' => '退款申请开始受理', 'contents' => '哇吖已受理退款申请，并会于近期与您取得联系');
                break;

            // 退款流程
            case 4:
                return array('status' => '申请被玩伴拒绝', 'contents' => '哇吖会将费用原路退回您的支付宝或微信');
                break;

            case 5:
                return array('status' => '申请被玩伴拒绝', 'contents' => '哇吖会将费用原路退回您的支付宝或微信');
                break;

            case 6:
                return array('status' => '已退款', 'contents' => '哇吖已成功将费用退回你的支付宝或微信');
                break;

            case 7:
                return array('status' => '已完成', 'contents' => '');
                break;

            default:
                return array('status' => '', 'contents' => '');
                break;
        }
    }

    public function getOrderBy($data)
    {
        $this->db->select('id,uid,moneyTotalRMB,title,shopId,goodsType,goodsId');
        $this->db->from(self::TABLE)->where($data);
        $data = $this->db->get()->result_array();
        return $data;
    }

}